package com.dhh.sky.activity;

import java.io.File;

import com.actionbarsherlock.view.MenuItem;
import com.dhh.sky.R;
import com.dhh.sky.util.Constant;
import com.dhh.sky.widget.DownloadAdapter;
import com.dhh.sky.widget.SlideMenuFragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class DownloadsActivity extends SlidingFragmentActivity{
	private ListView listView;
	private DownloadAdapter adapter;
	
	@Override 
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);		
		setBehindContentView(R.layout.menu_frame);
        setContentView(R.layout.downloads);
        
        getSupportFragmentManager().beginTransaction()
			.replace(R.id.menu_frame, new SlideMenuFragment()).commit();
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		initView();
		initActionBar();
	}
	
	private void initView(){
		listView = (ListView) this.findViewById(R.id.download_list);
		adapter = new DownloadAdapter(this);
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {				
			}});
	}
	
	private void initActionBar(){
		getSupportActionBar().setTitle(getString(R.string.title_downloads));
        getSupportActionBar().setHomeButtonEnabled(true);    
    	getSupportActionBar().setIcon(R.drawable.ic_app_menu);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);  		
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
	        case android.R.id.home:     
	        	toggle();
				return true;
        }
    	
        return false;
    }
	
	public void showDeleteDialog(final String fileName){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 		builder.setTitle(getString(R.string.title_dialog_delete));
 		builder.setMessage(getString(R.string.msg_delete_file));
 		builder.setPositiveButton(getString(R.string.label_ok), 
 				new OnClickListener() {
	            	@Override
	            	public void onClick(DialogInterface dialog, int which) {
	            		File file = new File(Constant.SKYDRIVE_LOCAL_PATH+"/"+fileName);
	            		if(file.exists() && file.canWrite()){
	            			file.delete();
	            			adapter.remove(fileName);
	            		}
	            		dialog.cancel();
	            	}
 				});
 		builder.setNegativeButton(getString(R.string.label_cancel), 
 				new OnClickListener() {
		             @Override
		             public void onClick(DialogInterface dialog, int which) {
		                dialog.cancel();
		             }
 				});
 		builder.create().show();
	}
}
